#include "car.h"
#include "ui_car.h"
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
#include<form.h>
#include<QMovie>
int max=0;
car::car(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::car),blsRun(false),speed(20),blsOver(false)
{

     vDirection=2;
    ui->setupUi(this);
    this->setGeometry(QRect(350,50,290,310));
    this->resize( QSize( 500, 600 ));
    vcar1.resize(100);
      vcar2.resize(100);
      slow.resize(100);
      scores.resize(100);
      acel.resize(100);

}

car::~car()
{
    delete ui;
}

void car::paintEvent(QPaintEvent *event){
    QPainter painter(this);




if(!blsOver)
    ui->pushButton_3->setVisible(false);
   if(blsOver){
        ui->pushButton_3->setVisible(true);
    ui->pushButton->setVisible(false);
max=score;
    }






    if(!blsRun){
initcar();}


    painter.drawRect(5,5,490,590);
    painter.drawPixmap(5,5,490,590,QPixmap(":/myimg/picture/9874.png"));

if(!blsOver){
if(turn){

    switch(mysw)
    {
    case 1:
    painter.setPen(Qt::transparent);
    painter.setBrush(Qt::transparent);
       painter.drawRect(mycar);
       painter.drawPixmap(mycar,QPixmap(":/myimg/picture/98714.png"));
        break;
    case 2:
       painter.setPen(Qt::transparent);
       painter.setBrush(Qt::transparent);
          painter.drawRect(mycar);
          painter.drawPixmap(mycar,QPixmap(":/myimg/picture/44646.png"));
        break;
    case 3:
        painter.setPen(Qt::transparent);
        painter.setBrush(Qt::transparent);
           painter.drawRect(mycar);
           painter.drawPixmap(mycar,QPixmap(":/myimg/picture/123654.png"));
        break;
}


    QString myDisplay=" 历史最高纪录:";
             QFont font6("华文楷体",15);
              painter.setFont(font6);
              painter.setPen(Qt::yellow);
              painter.setBrush(Qt::yellow);
              painter.drawText(300,20,myDisplay);
           QFont font8("华文楷体",15);
              painter.setFont(font8);
              painter.setPen(Qt::yellow);
              painter.setBrush(Qt::yellow);
              //painter.drawText(140,300,scoreLabel);
              painter.drawText(430,20,QString::number(max));

if(score>0&&score%70==0&&score>tmpp1)
{
    if(!judge3[m])
    {  judge3[m]=1;
       slow[m]=Creatcar3();
display1=1;
       }
    tmpp1=score;
}

if(score>0&&score%30==0&&score>tmpp2)
{
    if(!judge4[b])
    {  judge4[b]=1;
      scores[b]=Creatcar3();
      display2=1;

       }
    tmpp2=score;
}
if(score>0&&score%60==0&&score>tmpp4)
{
    if(!judge5[a])
    {  judge5[a]=1;
      acel[a]=Creatcar5();
      display5=1;

       }
    tmpp4=score;
}
painter.setPen(Qt::transparent);
painter.setBrush(Qt::transparent);
     painter.drawRect(slow[m]);
      painter.drawPixmap(slow[m],QPixmap(":/myimg/picture/yaoshui.png"));



      painter.setPen(Qt::transparent);
      painter.setBrush(Qt::transparent);
           painter.drawRect(  scores[b]);
            painter.drawPixmap(scores[b],QPixmap(":/myimg/picture/jiangli.png"));

            painter.setPen(Qt::transparent);
            painter.setBrush(Qt::transparent);
                 painter.drawRect(acel[a]);
                  painter.drawPixmap(acel[a],QPixmap(":/myimg/picture/jiasu.png"));

       for(int i=0;i<100;i++)
       {
           IsHit();
           if(blsOver)
           timer->stop();
     if(!judge[i])
     {  judge[i]=1;
       vcar1[i]=Creatcar();
     }
     if(!judge2[i])
     {  judge2[i]=1;
       vcar2[i]=Creatcar2();
     }



if(i%3==0){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar1[i]);
        painter.drawPixmap(vcar1[i],QPixmap(":/myimg/picture/441.png"));
}
if(i%3==1){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar1[i]);
        painter.drawPixmap(vcar1[i],QPixmap(":/myimg/picture/442.png"));
}
if(i%3==2){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar1[i]);
        painter.drawPixmap(vcar1[i],QPixmap(":/myimg/picture/443.png"));
}
 //
if(i%3==2){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar2[i]);
        painter.drawPixmap(vcar2[i],QPixmap(":/myimg/picture/441.png"));
}
if(i%3==1){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar2[i]);
        painter.drawPixmap(vcar2[i],QPixmap(":/myimg/picture/442.png"));
}
if(i%3==0){
     painter.setPen(Qt::transparent);
     painter.setBrush(Qt::transparent);
      painter.drawRect(vcar2[i]);
        painter.drawPixmap(vcar2[i],QPixmap(":/myimg/picture/443.png"));
}



       if(j==1){
      timer=new QTimer(this);
            timer->start(speed);
            connect(timer,SIGNAL(timeout()),SLOT(vcar_update()));
            j++;
       }
       on_lcdNumber_overflow();
}

      int tscore=tmpp3;
for(int k=0;k<100;k++)
{
    if(tmp[k]!=0){
       tscore+=10;}
    if(tmp2[k]!=0)
        tscore+=10;

}
score=tscore;
 ace();

}
}

//turn的
   if(blsOver){

QString sDisplay=" 你的得分:";
       QFont font1("Courier",36);
          painter.setFont(font1);
          painter.setPen(Qt::red);
          painter.setBrush(Qt::red);
          painter.drawText(80,300,sDisplay);
       QFont font3("华文楷体",40);
          painter.setFont(font3);
          painter.setPen(Qt::red);
          painter.setBrush(Qt::red);
          //painter.drawText(140,300,scoreLabel);
          painter.drawText(340,300,QString::number(score));

          //
        painter.setPen(Qt::transparent);//设置画笔颜色
        painter.setBrush(Qt::transparent);//设置填充颜色
        if(xx==1){
       QRect rect1(170,200,200,250);
        last1=rect1;}
         painter.drawRect(last1);//画n个小方块  //gai
           painter.drawPixmap(last1,QPixmap(":/myimg/picture/4444444452.png"));

        painter.setPen(Qt::transparent);//设置画笔颜色
        painter.setBrush(Qt::transparent);//设置填充颜色
        if(xx==1)
            { QRect rect2(170,200,200,250); last2=rect2;}
         painter.drawRect(last2);//画n个小方块  //gai
           painter.drawPixmap(last2,QPixmap(":/myimg/picture/2344.png"));

           if(xx==1){
            timer2=new QTimer(this);//设定计时器
              timer2->start(20);//计时器时间是0.5s
              connect(timer2,SIGNAL(timeout()),SLOT(last_update()));//信号连接槽，发出信号的是timer，信号是timeout，执行的槽是Snake_update
              //connect(timer,SIGNAL(timeout()),SLOT(on_lcdNumber_overflow()));//信号连接槽，发出信号的是timer，信号是timeout，执行的槽是Snake_update
              xx++;
         }
}


}


void car::last_update()
{
    last1.setTop(last1.top()+3);
   last1.setBottom(last1.bottom()+3);
   last2.setTop(last2.top()-3);
  last2.setBottom(last2.bottom()-3);
  update();
}


void car::vcar_update()
{
    if(turn){
    for(i=0;i<100;i++){
    vcar1[i].setTop(vcar1[i].top()+mover);
    vcar1[i].setBottom(vcar1[i].bottom()+mover);
    vcar2[i].setTop(vcar2[i].top()+mover);
    vcar2[i].setBottom(vcar2[i].bottom()+mover);
}
    if(display1){
   slow[m].setTop(slow[m].top()+3);
    slow[m].setBottom(slow[m].bottom()+3);
}
    if(display2){
    scores[b].setTop(scores[b].top()+3);
      scores[b].setBottom(scores[b].bottom()+3);
    }

    if(display5){
   acel[a].setTop(acel[a].top()+3);
      acel[a].setBottom(acel[a].bottom()+3);
    }
}

update();
    }





void car::initcar()
{  blsRun=true;
    QRect rect(250,350,42,80);//车的初始位置
        mycar=rect;


  }
void car::car_update(){

if(turn){
    if(mycar.left()!=0&&mycar.right()!=290)
    {
    switch(nDirection)
    {
    case 1:
        mycar.setTop(mycar.top()-myspeed);
        mycar.setBottom(mycar.bottom()-myspeed);
        break;
    case 2:
        mycar.setTop(mycar.top()+myspeed);
        mycar.setBottom(mycar.bottom()+myspeed);
        break;
    case 3:
        mycar.setLeft(mycar.left()-myspeed);
        mycar.setRight(mycar.right()-myspeed);
        break;
    case 4:
        mycar.setLeft(mycar.left()+myspeed);
        mycar.setRight(mycar.right()+myspeed);
        break;
    default:;

    }

    }
    else
        blsOver=true;


update();//paintEvent更新
}
}


void car::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_Up:nDirection=1;   //1代表上
        break;
    case Qt::Key_Down:nDirection=2;   //2代表下
        break;
    case Qt::Key_Left:nDirection=3;   //3代表左
        break;
    case Qt::Key_Right:nDirection=4;   //4代表右
        break;
    default:;
    }
    car_update();
}



QRect car::Creatcar(){
    int x,y;
    x=rand()%4;//生成小于4的整数
    y=rand()%2;

    QRect rect(70+x*105,-count,40,80);
count+=150*(y+1);
    return rect;
}

QRect car::Creatcar2(){
    int x,y;

    x=rand()%4;
    y=rand()%2;

    QRect rect(70+x*105,-count,40,120);
count+=150*(y+1);
    return rect;
}

QRect car::Creatcar3()
{
    int x,y;
    x=rand()%4;//生成小于25的整数


    QRect rect(70+x*105,0,60,60);

    return rect;


}
QRect car::Creatcar5()
{
    int x,y;

    x=rand()%4;


    QRect rect(70+x*105,-50,60,60);

    return rect;


}


QRect car::Creatcar4()
{
    int x,y;

    x=rand()%4;


    QRect rect(65+x*105,-40,60,78);

    return rect;


}
void car::IsHit(){
    for(int i=0;i<vcar1.size();i++){
        if(mycar.top()<=vcar1[i].bottom()&&mycar.top()>=vcar1[i].top()&&mycar.right()<=vcar1[i].right()&&mycar.right()>=vcar1[i].left()){
            blsOver=true;            
        }
        if(mycar.top()<=vcar1[i].bottom()&&mycar.top()>=vcar1[i].top()&&mycar.left()<=vcar1[i].right()&&mycar.left()>=vcar1[i].left()){
            blsOver=true;
                    }

        if(mycar.bottom()>=vcar1[i].top()&&mycar.bottom()<=vcar1[i].bottom()&&mycar.right()<=vcar1[i].right()&&mycar.right()>=vcar1[i].left()){

            blsOver=true;

        }
        if(mycar.bottom()>=vcar1[i].top()&&mycar.bottom()<=vcar1[i].bottom()&&mycar.left()<=vcar1[i].right()&&mycar.left()>=vcar1[i].left()){

            blsOver=true;

        }


        if(mycar.top()<=vcar2[i].bottom()&&mycar.top()>=vcar2[i].top()&&mycar.right()<=vcar2[i].right()&&mycar.right()>=vcar2[i].left()){

            blsOver=true;

        }
        if(mycar.top()<=vcar2[i].bottom()&&mycar.top()>=vcar2[i].top()&&mycar.left()<=vcar2[i].right()&&mycar.left()>=vcar2[i].left()){

            blsOver=true;

        }

        if(mycar.bottom()>=vcar2[i].top()&&mycar.bottom()<=vcar2[i].bottom()&&mycar.right()<=vcar2[i].right()&&mycar.right()>=vcar2[i].left()){

            blsOver=true;

        }
        if(mycar.bottom()>=vcar2[i].top()&&mycar.bottom()<=vcar2[i].bottom()&&mycar.left()<=vcar2[i].right()&&mycar.left()>=vcar2[i].left()){

            blsOver=true;

        }






        if(mycar.top()<=slow[m].bottom()&&mycar.top()>=slow[m].top()&&mycar.right()<=slow[m].right()&&mycar.right()>=slow[m].left()){
            m++;
                   if(mover>=2)
                  mover-=1;
                   display1=0;
        }
        if(mycar.top()<=slow[m].bottom()&&mycar.top()>=slow[m].top()&&mycar.left()<=slow[m].right()&&mycar.left()>=slow[m].left()){
            m++;
                   if(mover>=2)
                  mover-=1;
                   display1=0;
        }

        if(mycar.bottom()>=slow[m].top()&&mycar.bottom()<=slow[m].bottom()&&mycar.right()<=slow[m].right()&&mycar.right()>=slow[m].left()){
            m++;
                   if(mover>=2)
                  mover-=1;
                   display1=0;
        }
        if(mycar.bottom()>=slow[m].top()&&mycar.bottom()<=slow[m].bottom()&&mycar.left()<=slow[m].right()&&mycar.left()>=slow[m].left()){
            m++;
                   if(mover>=2)
                  mover-=1;
                   display1=0;
        }
        //
        if(mycar.top()<=scores[b].bottom()&&mycar.top()>=scores[b].top()&&mycar.right()<=scores[b].right()&&mycar.right()>=scores[b].left()){

                    tmpp3+=20;
                    display2=0;
                    b++;
        }
        if(mycar.top()<=scores[b].bottom()&&mycar.top()>=scores[b].top()&&mycar.left()<=scores[b].right()&&mycar.left()>=scores[b].left()){

                    tmpp3+=20;
                    display2=0;
                    b++;
        }

        if(mycar.bottom()>=scores[b].top()&&mycar.bottom()<=scores[b].bottom()&&mycar.right()<=scores[b].right()&&mycar.right()>=scores[b].left()){

                    tmpp3+=20;
                    display2=0;
                    b++;
        }

        if(mycar.bottom()>=scores[b].top()&&mycar.bottom()<=scores[b].bottom()&&mycar.left()<=scores[b].right()&&mycar.left()>=scores[b].left()){
            b++;
                    tmpp3+=20;
                    display2=0;
        }
//
        if(mycar.top()<=acel[a].bottom()&&mycar.top()>=acel[a].top()&&mycar.right()<=acel[a].right()&&mycar.right()>=acel[a].left()){

                   myspeed+=1;
                    display5=0;
                    a++;
        }
        if(mycar.top()<=acel[a].bottom()&&mycar.top()>=acel[a].top()&&mycar.left()<=acel[a].right()&&mycar.left()>=acel[a].left()){

            myspeed+=1;
             display5=0;
             a++;
        }

        if(mycar.bottom()>=acel[a].top()&&mycar.bottom()<=acel[a].bottom()&&mycar.right()<=acel[a].right()&&mycar.right()>=acel[a].left()){

            myspeed+=1;
             display5=0;
             a++;
        }
        if(mycar.bottom()>=acel[a].top()&&mycar.bottom()<=acel[a].bottom()&&mycar.left()<=acel[a].right()&&mycar.left()>=acel[a].left()){
            myspeed+=1;
             display5=0;
             a++;
        }

        if(vcar1[i].top()>mycar.bottom())
          {
              tmp[i]=1;

          }
        if(vcar2[i].top()>mycar.bottom())
          {
              tmp2[i]=1;

          }

if(mycar.right()>=500)
{blsOver=true;
}
    }


}





void car::ace()
{
    if(score>0&&score/50==n&&tmp4[n]==0)
       {
        mover+=1;
tmp4[n]=1;
n++;
}
}



void car::on_pushButton_clicked()
{

    turn=!turn;

}


void car::on_lcdNumber_overflow()
{
    ui->lcdNumber->setBinMode();
  ui->lcdNumber->setDigitCount(10);
  QString s=QString::number(score);
  ui->lcdNumber->setStyleSheet("border: 1px solid yellow; color: yellow;");
ui->lcdNumber->display(s);



}


void car::on_pushButton_3_clicked()
{

    Form *o=new Form;
    o->show();
}

